package logic

import (
	"context"

	"book/common/errorx"
	"book/service/sstwords/cmd/api/internal/svc"
	"book/service/sstwords/cmd/api/internal/types"
	"book/service/sstwords/cmd/rpc/sstwords"

	"github.com/tal-tech/go-zero/core/logx"
)

type SstwordsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewSstwordsLogic(ctx context.Context, svcCtx *svc.ServiceContext) SstwordsLogic {
	return SstwordsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *SstwordsLogic) Sstwords(req types.SstwordsReq) (resp interface{}, err error) {
	// 调用rpc服务
	res, err := l.svcCtx.SstwordsRpc.GetSstwords(l.ctx, &sstwords.Request{
		Keywords: req.Keywords,
	})

	if err != nil {
		return errorx.NewDefaultError(err.Error()), nil
	}

	return errorx.NewCodeSuccess(&types.SstwordsResp{
		Title: res.Title,
	}, "success"), nil
}
